import baseConfig from "./config.js";

const host = baseConfig.host;

const request = {
  // POST请求
  post: function post(url, parm) {
    return new Promise((resolve, reject) => {
      const token = uni.getStorageSync("token");
      let header = {
        "content-type": "application/json",
      };
      if (token) {
        header["Authorization"] = "Bearer " + uni.getStorageSync("token");
      }

      uni.request({
        url: host + url,
        method: "POST",
        data: parm,
        header: header,
        success: (res) => {
          // http200 成功
          if (res.statusCode == 200) {
            // 接口0成功，根据需要调整
            if (res.data.code == 200) {
              resolve(res.data);
            } else if (res.data.code == 401) {
              uni.showToast({
                icon: "none",
                duration: 2000,
                title: "登录过期，请重新登录",
                success() {
                  uni.removeStorage({
                    key: "token",
                  });
                  setTimeout(function () {
                    uni.redirectTo({
                      url: "/pages/public/login",
                    });
                  }, 500);
                },
              });
            } else {
              console.log(`res.data.message`,res.data.message)
              uni.showToast({
                title: res.data.message,
                icon: "none",
              });
            }
          } else {
            uni.showToast({
              title: "网络开小差了，请稍后再试",
              icon: "none",
              duration: 2000,
            });
          }
        },
        fail: (error) => {
          uni.showToast({
            title: "网络开小差了，请稍后再试",
            icon: "none",
            duration: 2000,
          });
          reject(error);
        },
      });
    });
  },

  // GET请求
  get: function get(url, parm) {
    return new Promise((resolve, reject) => {
      const token = uni.getStorageSync("token");
      let header = {
        "content-type": "application/x-www-form-urlencoded",
      };
      if (token) {
        header["Authorization"] = "Bearer " + uni.getStorageSync("token");
      }
      uni.request({
        url: host + url,
        method: "GET",
        data: parm,
        header: header,
        success: (res) => {
          if (res.statusCode == 200) {
            if (res.data.code == 200) {
              resolve(res.data);
            } else if (res.data.code == 401) {
              uni.showToast({
                icon: "none",
                duration: 2000,
                title: "登录过期，请重新登录",
                success() {
                  uni.removeStorage({
                    key: "token",
                  });
                  setTimeout(function () {
                    uni.redirectTo({
                      url: "/pages/public/login",
                    });
                  }, 500);
                },
              });
            } else {
              uni.showToast({
                title: res.data.message,
                icon: "none",
              });
            }
          } else {
            uni.showToast({
              title: "网络开小差了，请稍后再试",
              icon: "none",
            });
          }
        },
        fail: (error) => {
          uni.showToast({
            title: "网络开小差了，请稍后再试",
            icon: "none",
            duration: 2000,
          });
          reject(error);
        },
      });
    });
  },
};

export default request;
